/**
 * ...
 * @author Axel Huizinga - axel@cunity.me
 */

package;

import flash.display.GradientType;
import flash.filters.ColorMatrixFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import me.cunity.animation.sandy.STween;
import me.cunity.animation.TimeLine;
//import me.cunity.animation.sandy.TimeLine;
import me.cunity.tools.sandy.BitmapResource;
import sandy.core.data.Point3D;
import sandy.core.scenegraph.Shape3D;
import sandy.extrusion.data.Lathe;
import sandy.extrusion.data.Polygon2D;
import sandy.extrusion.Extrusion;
import sandy.HaxeTypes;
import sandy.materials.Appearance;
import sandy.materials.attributes.MaterialAttributes;
import sandy.materials.attributes.PhongAttributes;
import sandy.materials.BitmapMaterial;
import sandy.primitive.Plane3D;

class Ring
{
	var height:Float;
	var width:Float;
	var name:String;
	var filter:Bool;
	var filterExt:Extrusion;
	var ringmaterial:BitmapMaterial;

	public function new(n:String, h:Float, w:Float) 
	{
		//super(name, h, w, 1, 1 , Plane3D.ZX_ALIGNED);
		height = h;
		width = w;
		name = n;
	}
	
	public function draw(f:Bool=false):Shape3D
	{
		//LIGHT
		var _attrs: PhongAttributes = new PhongAttributes(true);		
		_attrs.diffuse = 0.8;
		_attrs.ambient = 0;
		_attrs.specular = 0.45;
		_attrs.gloss = 15;
		
		var arrayElm:Array<Point> = [
			//new Point(-40,0),
			new Point(-100,0),
			new Point( 0, 0)/*,
			new Point( 0, 1),
			new Point( -100, 1),
			new Point(-100,0)*/
		];
		
		var ringpol:Polygon2D = new Polygon2D(arrayElm);
		
		var ring:Lathe = new Lathe(
			new Point3D(), //center
			//new Point3D(1, 1, 1),//axis
			new Point3D(0, 1, 0),//axis
			//new Point3D( -1, -1, -1),//reference
			new Point3D( 1, 0,  0),//reference
			//-0.0169,//angle0
			0,//angle0
			Math.PI*2, //angle1
			0.10, // step
			18, //radius1
			18, //radius2
			0.0, //height0
			0.0, //height1
			.20, //scale0
			.20 //scale1
		);

		ringmaterial = new BitmapMaterial( 
			//new BitmapResource('RingMap').bmp.bitmapData, new MaterialAttributes([_attrs])
			new BitmapResource('RingMap').bmp.bitmapData
		);
		//ringmaterial.smooth = true;
		ringmaterial.alpha = 0;
		//ringmaterial.lightingEnable = true;
		var ext:Extrusion = new Extrusion(name, ringpol, ring.toSections(true), true, true);
		//ext.enableBackFaceCulling = true;
		//var app:Appearance = new Appearance(ringmaterial, ringmaterial);
		var app:Appearance = new Appearance(ringmaterial);
		ext.appearance = app;

		if (f) {
			filter = true;
			filterExt = ext;
			var matrix:Array<Dynamic> = new Array();
			matrix = matrix.concat([1, 0, 0, 1, 0]); // red
			matrix = matrix.concat([0, 1, 0, 1, 0]); // green
			matrix = matrix.concat([0, 0, 1, 1, 0]); // blue
			matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha

			ext.container.filters = [new ColorMatrixFilter(matrix)];	
			//ext.container.alpha = 0;
		}
		else
			ext.useSingleContainer = false;
		ext.rotateY = 90;
		return ext;
		
	}
	
	public function endShow(tL:TimeLine)
	{
		trace(filterExt);
		trace(filterExt.container.alpha);
	}
	
	public function startShow(cB:Dynamic)
	{
		trace(cB);
		STween.add(show, 1500, { values:[.48, 0], onComplete:cB } );
	}
	
	function show(v:Float)
	{
		if (filter)
			filterExt.container.alpha = v;
		ringmaterial.alpha = v;
	}
}